/*
 * This file is part of aion-lightning <aion-lightning.org>.
 * 
 * aion-lightning is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * aion-lightning is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with aion-lightning.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.controllers;

import com.aionengine.gameserver.ai2.event.AIEventType;
import com.aionengine.gameserver.ai2.follow.FollowStartService;
import com.aionengine.gameserver.dataholders.DataManager;
import com.aionengine.gameserver.model.TaskId;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.summons.UnsummonType;
import com.aionengine.gameserver.model.templates.npcskill.NpcSkillTemplates;

/**
 * @author xTz
 */
public class SiegeWeaponController extends SummonController {

    private NpcSkillTemplates skills;

    public SiegeWeaponController(int npcId) {
        skills = DataManager.NPC_SKILL_DATA.getNpcSkillList(npcId);
    }

    @Override
    public void release(final UnsummonType unsummonType) {
        getMaster().getController().cancelTask(TaskId.SUMMON_FOLLOW);
        getOwner().getMoveController().abortMove();
        super.release(unsummonType);
    }

    @Override
    public void restMode() {
        getMaster().getController().cancelTask(TaskId.SUMMON_FOLLOW);
        super.restMode();
        getOwner().getAi2().onCreatureEvent(AIEventType.STOP_FOLLOW_ME, getMaster());
    }

    @Override
    public void setUnkMode() {
        super.setUnkMode();
        getMaster().getController().cancelTask(TaskId.SUMMON_FOLLOW);
    }

    @Override
    public final void guardMode() {
        super.guardMode();
        getMaster().getController().cancelTask(TaskId.SUMMON_FOLLOW);
        getOwner().setTarget(getMaster());
        getOwner().getAi2().onCreatureEvent(AIEventType.FOLLOW_ME, getMaster());
        getOwner().getMoveController().moveToTargetObject();
        getMaster().getController().addTask(TaskId.SUMMON_FOLLOW, FollowStartService.newFollowingToTargetCheckTask(getOwner(), getMaster()));
    }

    @Override
    public void attackMode(int targetObjId) {
        super.attackMode(targetObjId);
        Creature target = (Creature) getOwner().getKnownList().getObject(targetObjId);
        if (target == null) {
            return;
        }
        getOwner().setTarget(target);
        getOwner().getAi2().onCreatureEvent(AIEventType.FOLLOW_ME, target);
        getOwner().getMoveController().moveToTargetObject();
        getMaster().getController().addTask(TaskId.SUMMON_FOLLOW, FollowStartService.newFollowingToTargetCheckTask(getOwner(), target));
    }

    @Override
    public void onDie(final Creature lastAttacker) {
        getMaster().getController().cancelTask(TaskId.SUMMON_FOLLOW);
        super.onDie(lastAttacker);
    }

    public NpcSkillTemplates getNpcSkillTemplates() {
        return skills;
    }
}
